package pt.ipleiria.plp.ui;

import de.enough.polish.ui.Alert;
import de.enough.polish.ui.AlertType;
import de.enough.polish.ui.ChoiceItem;
import de.enough.polish.ui.Command;
import de.enough.polish.ui.Displayable;
import de.enough.polish.ui.List;
import de.enough.polish.util.Locale;
import pt.ipleiria.plp.BlackSpotterMidlet;
import pt.ipleiria.plp.Lixeiras.LixeiraPeddingComplete;
import pt.ipleiria.plp.Lixeiras.LixeiraSent;
import pt.ipleiria.plp.threads.CallbackConfirm;
import pt.ipleiria.plp.threads.DeleteLixeiraFromServer;

/**
 *
 * @author filipe
 */
public class RecievedEventosScreen extends VerEventosScreen implements CallbackConfirm {

    LixeiraSent[] lixs;
    private OnHold hold;
    public RecievedEventosScreen(String title,BlackSpotterMidlet midlet,
            LixeiraSent[] lixs) {
        super(title, List.EXCLUSIVE, midlet);

        this.lixs = lixs;

        populateList();
    }

    public void populateList() {

        for (int i = 0; i < lixs.length; i++) {
            //#style mainItem
            ItemLix itl = new ItemLix(lixs[i].getTitle(), lixs[i].getGuid(), true,
                    null, ChoiceItem.HORIZONTAL & ChoiceItem.LAYOUT_CENTER);

            int index = this.append(itl);
            this.getItem(index).addCommand(getCmdDel());
            this.getItem(index).addCommand(getCmdEdit());
        }

    }

    public void selectListItem() {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == getCmdEdit()) {
            int selectedIndex = this.getSelectedIndex();
            ItemLix temp = (ItemLix) this.getItem(selectedIndex);
            getMidlet().editLixeira(temp.getGuid(), temp.isEnviada());

        } else if (c == getCmdMenu()) {
            getMidlet().getDisplay().setCurrent(getMidlet().getMainMenu());
        } else if (c == getCmdDel()) {

            int selectedIndex = this.getSelectedIndex();
            ItemLix temp = (ItemLix) this.getItem(selectedIndex);
            DeleteLixeiraFromServer t = new DeleteLixeiraFromServer(temp.getGuid(),
                    temp.isEnviada(), getMidlet(), this);
            t.start();
            hold = new OnHold(getMidlet(), this);
            getMidlet().getDisplay().setCurrent(hold);

        } else {
            super.commandAction(c, d);
        }
    }

    public void returnConfimation(String error) {
        populateList();
        if (error == null) {

            Alert alert = new Alert(Locale.get("title.sucesso"),
                    Locale.get("mensagens.sucesso"), null, AlertType.INFO);
            getMidlet().getDisplay().setCurrent(alert, getMidlet().getMainMenu());

        } else {

            Alert alert = new Alert(Locale.get("title.Erro"),
                    error, null, AlertType.ERROR);
            getMidlet().getDisplay().setCurrent(alert, getMidlet().getMainMenu());

        }

    }
}
